<?php

/**
 *    Desc
 *
 *    @author    Garbin
 *    @usage    none
 */
class Fabric_shopApp extends MemberbaseApp
{
	var $_fs_mod;
	var $_ft_mod;
	
    function __construct()
    {
        $this->Fabric_shopApp();
    }
    function Fabric_shopApp()
    {
        parent::__construct();
		$this->_fs_mod =& m('fabric_shop');
		$this->_ft_mod =& m('fabric_type');
    }
    function index()
    {
		/* 检查权限 */
		$this->check_powers(USER_ADMIN);
		
		$page = $this->_get_page();
		$fabric_shops = $this->_fs_mod->find(array(
			'order' => 'fabric_shop_name ASC',
			'count' => true,
			'limit' => $page['limit']
		));
		
		$page['item_count'] = $this->_fs_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('fabric_shops', $fabric_shops);
		
        $this->display('fabric_shop.index.html');
        /* 当前位置 */
        $this->_curlocal(LANG::get('fabric_shop_manage'));
    }
    
    
    //待激活档口
    function activation(){
    	$conditions=$this->_get_query_conditions(array(
    			array(
    					'field' => 'user_id',
    					'equal' => '=',
    					'name'	=> 'fabric_shop_id'
    			),
    			array(
    					'field' => 'ftype_id',
    					'equal' => '=',
    					'name'	=> 'fabric_type_id'
    			)
    	));
    	$page = $this->_get_page();
    	$user_mod=&m('member');
    	$fabric_type_mod=&m('fabric_type');
    	$order = empty($_GET['order']) ? 'user_name ASC' : str_replace('-', ' ', trim($_GET['order']));
    	if ($order != 'user_name ASC' || $order != 'user_name DESC') {
    		$order .= ', user_name ASC';
    	}
    	
    	$fabric_shop_list=$user_mod->find(array(
    			//'join'=>'has_merchant',
    			'conditions'=>'activation !=1 and role='.USER_MERCHANT,
    			'order' => 'user_name ASC',
    	));
    	$fabric_type_list=$this->_ft_mod->find(array(
    			'conditions'=>'if_show=1',
    			'order'=>'fabric_type_name asc'
    	));
    	
    	$fabric_shops = $user_mod->find(array(
    			//'join'=>'has_merchant',
				'fields'=>'reg_time,user_name,ftype_id,phone_mob,phone_tel,is_qq,wx_sn,email,user_id',
    			'conditions'=>'activation !=1 and role='.USER_MERCHANT.$conditions,
    			'order' => $order,
    			'count' => true,
    			'limit' => $page['limit']
    	));
    	$this->assign('fabric_types',$fabric_type_mod->get_options(-1));
    	$page['item_count'] = $user_mod->getCount();
    	$this->_format_page($page);
    	$this->assign('page_info', $page);
    	$this->assign('fabric_shops', $fabric_shops);
    	$this->assign('fabricshop_list',$fabric_shop_list);
    	$this->assign('fabrictype_list',$fabric_type_list);
    	$this->assign('user',$this->visitor->get());
    	$this->display('fabric_shop.activation.html');
    	/* 当前位置 */
    	$this->_curlocal(LANG::get('merchant_activation'));
    }
    
    //档口联络表
    function merch_list()
    {
    	$conditions=$this->_get_query_conditions(array(
    		array(
    			'field' => 'user_id',
                'equal' => '=',		
    			'name'	=> 'fabric_shop_id'	
    		),	
    		array(
    			'field' => 'ftype_id',
    			'equal' => '=',
    			'name'	=> 'fabric_type_id'
    		)
    	));
    	$page = $this->_get_page();
    	$user_mod=&m('member');
    	$fabric_type_mod=&m('fabric_type');
    	$fabric_shop_list=$user_mod->find(array(
    			'conditions'=>'activation =1 and role='.USER_MERCHANT,
    			'order' => 'user_name ASC',
    	));
    	$fabric_type_list=$this->_ft_mod->find(array(
    		'conditions'=>'if_show=1',
    		'order'=>'fabric_type_name desc'	
    	));
    	$order = empty($_GET['order']) ? 'user_name ASC' : str_replace('-', ' ', trim($_GET['order']));
    	if ($order != 'user_name ASC' || $order != 'user_name DESC') {
    		$order .= ', user_name ASC';
    	}
		$conditions='activation =1 and role='.USER_MERCHANT.$conditions;
    	$fabric_shops = $user_mod->get_fabric_list($conditions,$order,$page['limit']);
    	$this->assign('fabric_types',$fabric_type_mod->get_options(-1));
    	$page['item_count'] = $user_mod->getCount();
    	$this->_format_page($page);
    	$this->assign('page_info', $page);
    	$this->assign('fabric_shops', $fabric_shops);
    	$this->assign('fabricshop_list',$fabric_shop_list);
    	$this->assign('fabrictype_list',$fabric_type_list);
    	$this->assign('user',$this->visitor->get());
    	$this->display('fabric_shop.list.html');
    	/* 当前位置 */
    	$this->_curlocal(LANG::get('merchant_manage'));
    }
	
	function _assign_form()
	{
		$fabric_shop = array(
			'if_show' => 1
		);
		
		$this->assign('fabric_shop', $fabric_shop);
		$this->assign('fabric_types', $this->_get_options());
	}
	
	function add()
	{
		/* 检查权限 */
		$this->check_powers(USER_ADMIN);
		
		if(!IS_POST) {
			$this->_assign_form();
			
	        $this->display('fabric_shop.form.html');
        	$this->_curlocal(LANG::get('fabric_shop_manage'), 'index.php?app=fabric_shop', Lang::get('add_new_fabric_shop'));
		} else {
			$fabric_shop_name = trim($_POST['fabric_shop_name']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($fabric_shop_name)) {
				$this->show_warning('fabric_shop_name_not_empty');
				return;
			}
			if (!$this->_fs_mod->unique($fabric_shop_name)) {
				$this->show_warning('fabric_shop_is_exist');
				return;
			}
			
			$member_mod=&m('member');
			$user_data=array(
					'user_name'=>$fabric_shop_name,
					'password'=>md5(DEFAULT_MERCH_PASSWORD),
					'activation'=>0,
					'role'=>USER_MERCHANT,
					'ftype_id'=>$_POST['type_id'],
					'reg_time'=>gmtime()
			);
			$user_id=$member_mod->add($user_data);
			if (!$user_id){
				$this->_error($member_mod->get_error());
				return;
			}
			
			$data = array(
				'fabric_shop_name' => $fabric_shop_name,
				'description' => $description,
				'if_show' => $if_show,
				'type_id'=>$_POST['type_id']
			);
			if ($user_id){
				$data['user_id']=$user_id;
			}
			$fabric_shop_id = $this->_fs_mod->add($data);
			if (!$fabric_shop_id) {
				$this->_error($this->_fs_mod->get_error());
				return;
			}
			$this->show_message('add_fabric_shop_success', Lang::get('fabric_shop'), 'index.php?app=fabric_shop');
		}
	}
	
	function edit()
	{
		/* 检查权限 */
		$this->check_powers(USER_ADMIN);
		
		$fsid = intval($_GET['fsid']);
		if (!$fsid) {
			$this->show_warning('fabric_shop_not_exist');
			return;
		}
		if (!IS_POST) {
			$fabric_shop = $this->_fs_mod->get_info($fsid);
			$this->assign('fabric_shop', $fabric_shop);
			$this->assign('fabric_types', $this->_get_options());
	        $this->display('fabric_shop.form.html');
	        if ($this->visitor->get('role')==USER_ADMIN){
	        	$this->_curlocal(LANG::get('fabric_shop_manage'), 'index.php?app=fabric_shop', Lang::get('edit_fabric_shop'));
	        }else{
	        	$this->_curlocal(LANG::get('fabric_shop_manage'), 'index.php?app=fabric_shop&act=merch_list', Lang::get('edit_fabric_shop'));
	        }
		} else {
			$fabric_shop_name = trim($_POST['fabric_shop_name']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($fabric_shop_name)) {
				$this->show_warning('fabric_shop_name_not_empty');
				return;
			}
			if (!$this->_fs_mod->unique($fabric_shop_name, $fsid)) {
				$this->show_warning('fabric_shop_is_exist');
				return;
			}
			
			$data = array(
				'fabric_shop_name' => $fabric_shop_name,
				'description' => $description,
				'if_show' => $if_show,
				'type_id'=>$_POST['type_id']
			);
			$this->_fs_mod->edit($fsid, $data);
			if ($this->_fs_mod->get_error()) {
				$this->_error($this->_fs_mod->get_error());
				return;
			}
			if ($this->visitor->get('role')==USER_ADMIN){
				$this->show_message('edit_fabric_shop_success', Lang::get('fabric_shop'), 'index.php?app=fabric_shop');
			}else{
				$this->show_message('edit_fabric_shop_success', Lang::get('fabric_shop'), 'index.php?app=fabric_shop&act=merch_list');
			}
			
		}
	}
	
	function del()
	{
		$fsid = intval($_GET['fsid']);
		if (!$fsid) {
			$this->show_warning('fabric_shop_not_exist');
			return;
		}
	  $fs_info=$this->_fs_mod->get_info($fsid);
	  if (!$fs_info['user_id']){
	  	$this->show_warning('数据同步有错,请联系管理员');
	  	return;
	  }else{
	  	$user_mod=&m('member');
	  	$user_mod->drop("user_id={$fs_info['user_id']} and role=".USER_MERCHANT);
	  	if ($user_mod->get_error()) {
	  		$this->_error($user_mod->get_error());
	  		return;
	  	}
	  }
	  $this->_fs_mod->drop("fabric_shop_id={$fsid}");
	  if ($this->_fs_mod->get_error()) {
		  $this->_error($this->_fs_mod->get_error());
		  return;
	  }
	  $this->show_message('删除布行档口成功', Lang::get('fabric_shop'), 'index.php?app=fabric_shop');
	}
	
	
	//查看档口/布行
	function show(){
		$fsid = intval($_GET['fsid']);
		if (!$fsid) {
			$this->show_warning('fabric_shop_not_exist');
			return;
		}
		$user_mod=&m('member');
		$fs_info=$user_mod->get(array(
			'join'=>'has_merchant',	
			'conditions'=>'member.user_id='.$fsid
		));
		$fabric_type_mod=&m('fabric_type');
		$this->assign('fabric_types',$fabric_type_mod->get_options(-1));
		$this->assign('fs_info',$fs_info);
		$this->display('fabric_shop.show.html');
	}
	
	function check_name()
	{
		$fabric_shop_name = empty($_GET['fabric_shop_name']) ? null : trim($_GET['fabric_shop_name']);
		$fabric_shop_id = empty($_GET['fabric_shop_id']) ? 0 : intval($_GET['fabric_shop_id']);
		if (!$fabric_shop_name) {
            echo ecm_json_encode(false);
            return;
		}
		echo ecm_json_encode($this->_fs_mod->unique($fabric_shop_name, $fabric_shop_id));
		//echo $this->_fs_mod->unique($name, $id);
	}
	
	function AjaxAdd()
	{
		/* 检查权限 */
		if (!$this->check_powers(array(USER_MERCHANDISER, USER_ASSISTANT), false)) {
			echo ecm_json_encode(array('done'=>false,'msg'=>'您没有权限使用此功能！'));
			return;
		}
		if (IS_POST) {
			$fabric_shop_name = trim($_POST['fabric_shop_name']);
			if (!$fabric_shop_name) {
				echo ecm_json_encode(array('done'=>false,'msg'=>Lang::get('fabric_shop_name_not_empty')));
				return;
			}
			if (!$this->_fs_mod->unique($fabric_shop_name)) {
				echo ecm_json_encode(array('done'=>false,'msg'=>Lang::get('fabric_shop_is_exist')));
				return;
			}
			$data = array(
				'fabric_shop_name' => $fabric_shop_name,
				'if_show' => 1
			);
			$fabric_shop_id = $this->_fs_mod->add($data);
			if (!$fabric_shop_id) {
				echo ecm_json_encode(array('done'=>false,'msg'=>$this->_error($this->_fs_mod->get_error())));
				return;
			}
			echo ecm_json_encode(array('done'=>true,'msg'=>Lang::get('add_fabric_shop_success'),'fabric_shop_id'=>$fabric_shop_id));
		} else {
			echo ecm_json_encode(array('done'=>false,'msg'=>'请提交布行/档口名称！'));
		}
		
		/*if (!IS_POST) {
			$this->_assign_form();
			$this->display('fabric_shop.form.html');
		} else {
			$fabric_shop_name = trim($_POST['fabric_shop_name']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($fabric_shop_name)) {
				$this->show_warning('fabric_shop_name_not_empty');
				return;
			}
			if (!$this->_fs_mod->unique($fabric_shop_name)) {
				$this->show_warning('fabric_shop_is_exist');
				return;
			}
			
			$data = array(
				'fabric_shop_name' => $fabric_shop_name,
				'description' => $description,
				'if_show' => $if_show
			);
			$fabric_shop_id = $this->_fs_mod->add($data);
			if (!$fabric_shop_id) {
				$this->_error($this->_fs_mod->get_error());
				return;
			}
			
			echo '<script type="text/javascript">window.parent.ref_fabricshop("");window.parent.unblock();</script>';
		}*/
	}
	
	function AjaxRef()
	{
		$name = empty($_GET['name']) ? null : trim($_GET['name']);
		if ($name) {
			$conditions = " AND fabric_shop_name like '$name%'";
		}
		$list = $this->_fs_mod->find(array(
			'conditions' => 'if_show = 1' . $conditions,
			'fields' => 'fabric_shop_id, fabric_shop_name',
			'order' => 'fabric_shop_name ASC'
		));
		
		/* old code */
		/*$str = '';
		foreach ($list as $item) {
			$str .= $item['fabric_shop_id'] . ',' . $item['fabric_shop_name'] . ';';
		}
		echo substr($str, 0, strlen($str) - 1);*/
		
		/* new code */
		$data = array();
		foreach ($list as $item) {
			$temp = array($item['fabric_shop_id'], $item['fabric_shop_name']);
			$data[] = $temp;
		}
		echo ecm_json_encode($data);
	}
	
	
	/* 异步取下一级类型  */
	function ajax_cate()
	{
		if(!isset($_GET['id']) || empty($_GET['id']))
		{
			echo ecm_json_encode(false);
			return;
		}
		$cate = $this->_ft_mod->get_list($_GET['id']);
		foreach ($cate as $key => $val)
		{
			$child = $this->_ft_mod->get_list($val['fabric_type_id']);
			if (!$child || empty($child))
			{
				$cate[$key]['switchs'] = 0;
			}
			else
			{
				$cate[$key]['switchs'] = 1;
			}
		}
		header("Content-Type:text/html;charset=" . CHARSET);
		echo ecm_json_encode(array_values($cate));
		//$this->json_result($cate);
		return ;
	}
	
	//档口类型
	function stall_type(){
		$fabric_types = $this->_ft_mod->get_list(0);
		foreach ($fabric_types as $key => $val)
		{
			$fabric_types[$key]['switchs'] = 0;
			if ($this->_ft_mod->get_list($val['fabric_type_id']))
			{
				$fabric_types[$key]['switchs'] = 1;
			}
		}
		$this->import_resource(array('script' => 'inline_edit.js,jqtreetable.js',
				'style' => 'res:style/jqtreetable.css'));
		$this->assign('fabric_type_list', $fabric_types);
		$this->display('fabric_type.index.html');
		/* 当前位置 */
		$this->_curlocal(LANG::get('fabric_type_manage'));
	}
	
	
	/* 新增 */
	function add_type()
	{
		if (!IS_POST)
		{
			/* 参数 */
			$pid = empty($_GET['pid']) ? 0 : intval($_GET['pid']);
			$fabrics = array('parent_id' => $pid);
			$this->assign('fabrics', $fabrics);
	
			$this->assign('parents', $this->_get_options());
			$this->display('fabric_type.form.html');
		}
		else
		{
			$data = array(
					'fabric_type_name' => $_POST['fabric_type_name'],
					'parent_id' => $_POST['parent_id'],
			);
	
			/* 检查名称是否已存在 */
			if (!$this->_ft_mod->unique(trim($data['fabric_type_name']), $data['parent_id']))
			{
				$this->show_warning('name_exist');
				return;
			}
	
			/* 保存 */
			$type_id = $this->_ft_mod->add($data);
			if (!$type_id)
			{
				$this->show_warning($this->_ft_mod->get_error());
				return;
			}
	
			$this->show_message('add_ok',
					'back_list',    'index.php?app=fabric_shop&amp;act=stall_type',
					'continue_add', 'index.php?app=fabric_shop&amp;act=add_type&amp;pid=' . $data['parent_id']
			);
		}
	}
	
	/* 编辑 */
	function edit_type()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!IS_POST)
		{
			/* 是否存在 */
			$fabrics = $this->_ft_mod->get_info($id);
			if (!$fabrics)
			{
				$this->show_warning('type_empty');
				return;
			}
			$this->assign('fabrics', $fabrics);
	
			$this->assign('parents', $this->_get_options($id));
			$this->display('fabric_type.form.html');
		}
		else
		{
			$data = array(
					'fabric_type_name' => $_POST['fabric_type_name'],
					'parent_id'   => $_POST['parent_id'],
			);
				
			/* 检查名称是否已存在 */
			if (!$this->_ft_mod->unique(trim($data['fabric_type_name']), $data['parent_id'], $id))
			{
				$this->show_warning('name_exist');
				return;
			}
	
			/* 保存 */
			$rows = $this->_ft_mod->edit($id, $data);
			if ($this->_ft_mod->has_error())
			{
				$this->show_warning($this->_ft_mod->get_error());
				return;
			}
	
			$this->show_message('edit_ok',
					'back_list',    'index.php?app=fabric_shop&amp;act=stall_type',
					'edit_again',   'index.php?app=fabric_shop&amp;act=edit_type&amp;id=' . $id
			);
		}
	}
	
	
	/* 删除 */
	function drop_type()
	{
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$id)
		{
			$this->show_warning('no_type_to_drop');
			return;
		}
		$ids = explode(',', $id);
		if (!$this->_ft_mod->drop($ids))
		{
			$this->show_warning($this->_ft_mod->get_error());
			return;
		}
	
		$this->show_message('drop_ok');
	}
	
	
	/* 构造并返回树 */
	function &_tree($types)
	{
		import('tree.lib');
		$tree = new Tree();
		$tree->setTree($types, 'fabric_type_id', 'parent_id', 'fabric_type_name');
		return $tree;
	}
	
	/* 取得可以作为上级的类型数据 */
	function _get_options($except = NULL)
	{
		$types = $this->_ft_mod->get_list();
		$tree =& $this->_tree($types);
		return $tree->getOptions(MAX_LAYER - 1, 0, $except);
	}
}

?>
